package com.cyq.test0618.booksystem;

import java.util.Scanner;

public class Booksale {
    int orderIndex = 0;
    Book[] books = new Book[50];
    Order[] orders = new Order[50];
    Scanner sc = new Scanner(System.in);

    public Booksale() {
        books[0] = new Book(1, "星辰变", 35, 77);
        books[1] = new Book(2, "校花的贴身高手", 44, 30);
        books[2] = new Book(3, "九鼎记", 50, 70);
    }

    public void buyBook() {
        int oPrice = 0;
        String oDetails = "";
        for (int i = 0; i < 3; i++) {
            System.out.println("必须连续购买三次，当前第" + (i + 1) + "次");
            System.out.println("请输入你要购买的图书编号");
            showBook();
            int input = sc.nextInt();
            System.out.println("请输入你要购买的数量");
            int inputNum = sc.nextInt();
            for (int j = 0; j < books.length; j++) {
                if (books[j] != null) {
                    if (books[j].getBookNum() == input) {
                        books[j].setBookLeft(books[j].getBookLeft() - inputNum);
                        oPrice += books[j].getBookPrice() * inputNum;
                        oDetails += books[j].getBookName() + "," + books[j].getBookNum() + "本,";
                    }
                }
            }
        }
        System.out.println("请输入购书日期，格式为YYYY-MM-dd");
        String inputTime = sc.next();
        orders[orderIndex] = new Order();
        orders[orderIndex].setOrderNum(orderIndex + 1);//记录订单号
        orders[orderIndex].setOrderPrice(oPrice);//记录订单总额
        orders[orderIndex].setOrderDetails(oDetails);//记录订单明细
        orders[orderIndex].setOrderTime(inputTime);
        orderIndex++;

        System.out.println("请问是否继续购买？Y/N");
        String flag = sc.next();
        if ("Y".equals(flag)) {
            buyBook();
        } else if ("N".equals(flag)) {
            System.out.println("谢谢购买，以下是你购买的订单信息");
            showOrder();
        } else {
            System.out.println("输入有误");
            System.exit(0);
        }


    }

    public void showBook() {
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                System.out.println(books[i]);
            }
        }

    }

    public void showOrder() {
        for (int i = 0; i < orders.length; i++) {
            if (orders[i] != null) {
                System.out.println(orders[i]);
            }
        }

    }

    public void showByMoney() {
        System.out.println("排序后-------------------");
        for (int i = 0; i < orders.length - 1; i++) {
            for (int j = 0; j < orders.length - i - 1; j++) {
                if (orders[j] != null && orders[j + 1] != null) {
                    if (orders[j].getOrderPrice() > orders[j + 1].getOrderPrice()) {
                        Order temporder = orders[j];
                        orders[j] = orders[j + 1];
                        orders[j + 1] = temporder;
                    }
                }
            }
        }
        showOrder();
    }
}
